package com.greenhouse9.prog02;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.*;

import com.greenhouse9.prog02.Backup.BackupManager;
import com.greenhouse9.prog02.Backup.BackupResultDetail;
import com.greenhouse9.prog02.Backup.backup_result;


public class Main {
  public static void main(String[] args){

  	System.out.println( "�ϐ��̐���" + args.length + "�ł��B");

  	for (int i=0; i<args.length; i++){
  		System.out.println("�ϐ� arg[" + i + "] �� " + args[i] + " �ł��B");
  	}

  	/* ��̐����`�F�b�N */
  	if (args.length < 1) {
  		System.out.println("Usage: java Prog1 filename ");
  		System.exit(0);
  	}

  	File f1    = new File(args[0]);

  	/* Check file exists */
  	if (!f1.exists()) {
  		System.out.println(f1.getName() + "�͑��݂��܂���B");
  	}
  	else {
  		System.out.println("�t�@�C����: "   + f1.getName() + " �ł��B");
  		System.out.println("�p�X��:�@"      + f1.getPath() + "�@�ł��B");
  		System.out.println("��΃p�X���F�@" + f1.getAbsolutePath() );
  		System.out.println("�e�p�X���F�@"   + f1.getParent());
  		try {
  			System.out.println("���K�p�X���F�@" + f1.getCanonicalPath() );
  		}
  		catch (IOException e){
  			e.printStackTrace();
  		}
  		if (f1.isFile()) {
  			System.out.println(f1.getName() +"�@�̓t�@�C���ł��B");
  		} else if (f1.isDirectory()){
  			System.out.println(f1.getName() +" �̓f�B���N�g���ł��B");
  		}
  		if (f1.canRead()){
  			System.out.println(f1.getName() + "�@�͓ǂݍ��߂܂��B");
  		}
  		if (f1.canWrite()){
  			System.out.println(f1.getName() + "�@�͏������߂܂��B");
  		}
  		System.out.println("�t�@�C�����F " + f1.length());
  		if (f1.isDirectory()){
  			String [] filelist;
  		    filelist = f1.list();
  		    for (int i=0; i< filelist.length; i++){
  			    System.out.println(filelist[i]);
  		    }
  		}

  		System.out.println("BackupMangaer start:");
  		try {
  			String filename = f1.getCanonicalPath();
  			BackupManager.parseBackup(filename);
  		}
  		catch (IOException e) {
  			e.printStackTrace();
  		}

  		backup_result br2 = BackupManager.getBackup(1);

  		System.out.println(br2.getBackupNo());
  		System.out.println(br2.getBackupName());
  		System.out.println(br2.getDetailNum());
  		System.out.println(br2.getTotalBytes());

        BackupResultDetail brd = br2.getDetail(1);

  		System.out.println(brd.getBackupNo());
  		System.out.println(brd.getSeq());
  		System.out.println(brd.getFileName());
  		System.out.println(brd.getFileSize());

        BackupManager.readReal("Disc01");

        ArrayList<backup_result> al_br = BackupManager.getReal();

        br2 = al_br.get(0);

        brd = br2.getDetail(2);

  		//System.out.println(brd.getBackupNo());
  		System.out.println(brd.getSeq());
  		System.out.println(brd.getFileName());
  		System.out.println(brd.getFileSize());

        HashMap<Integer, com.greenhouse9.prog02.Backup.BackupResultDetail> m_brd = br2.getDetail();

  		for (Integer i: m_brd.keySet()){
  			brd = m_brd.get(i);

  			//System.out.println(brd.getBackupNo());
  		    System.out.print(brd.getSeq() + " ");
  		    System.out.print(brd.getFileName() + " ");
  		    System.out.println(brd.getFileSize());

  		}

  		//���C������
		System.out.println("������I�����ĉ������B");
		System.out.println("[1] �o�b�N�A�b�v��ǉ�");
		System.out.println("[2] �o�b�N�A�b�v��胊�X�g");
		System.out.println("[3] �o�b�N�A�b�v���ѓo�^");
  		System.out.println("[4] �o�b�N�A�b�v���ڎ��ѓo�^");
  		System.out.println("[5] �o�b�N�A�b�v���у��X�g");
  		System.out.println("[6] �o�b�N�A�b�v���т�����");
		System.out.println("[9] �I��");

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

  		try {

  			String line = reader.readLine();
			char c = line.charAt(0);

  			switch(c){
			case 'a':
			case '1':
				//System.out.println("�I�����W�W���[�X�ł��B");
				break;
			case 'b':
			case '2':
				//System.out.println("�R�[�q�[�ł��B");
				break;
  			case '4':
  				System.out.println("�o�b�N�A�b�v�ς̃��[�g�f�B���N�g�����͂��ĉ������B");
  				line = reader.readLine();
  				c = line.charAt(0);
  				break;
  			case '9':
  				System.exit(0);
			default:
				//System.out.println("�ǂ���ł�����܂���B");
				break;
			}

  		} catch (IOException e){
  		}

  	}
  }
}
